home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / directx / dxf / samples / multimedia / directshow / baseclasses / winctrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  65.6 KB  |  2,053 lines

  1. //------------------------------------------------------------------------------
  2. // File: WinCtrl.cpp
  3. //
  4. // Desc: DirectShow base classes - implements video control interface class.
  5. //
  6. // Copyright (c) 1992 - 2000, Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <streams.h>
  11.  
  12. // The control interface methods require us to be connected
  13.  
  14. #define CheckConnected(pin,code)                    \
  15. {                                                   \
  16.     if (pin == NULL) {                              \
  17.         ASSERT(!TEXT("Pin not set"));               \
  18.     } else if (pin->IsConnected() == FALSE) {       \
  19.         return (code);                              \
  20.     }                                               \
  21. }
  22.  
  23. // This checks to see whether the window has a drain. An application can in
  24. // most environments set the owner/parent of windows so that they appear in
  25. // a compound document context (for example). In this case, the application
  26. // would probably like to be told of any keyboard/mouse messages. Therefore
  27. // we pass these messages on untranslated, returning TRUE if we're successful
  28.  
  29. BOOL WINAPI PossiblyEatMessage(HWND hwndDrain, UINT uMsg, WPARAM wParam, LPARAM lParam)
  30. {
  31.     if (hwndDrain != NULL && !InSendMessage())
  32.     {
  33.         switch (uMsg)
  34.         {
  35.             case WM_CHAR:
  36.             case WM_DEADCHAR:
  37.             case WM_KEYDOWN:
  38.             case WM_KEYUP:
  39.             case WM_LBUTTONDBLCLK:
  40.             case WM_LBUTTONDOWN:
  41.             case WM_LBUTTONUP:
  42.             case WM_MBUTTONDBLCLK:
  43.             case WM_MBUTTONDOWN:
  44.             case WM_MBUTTONUP:
  45.             case WM_MOUSEACTIVATE:
  46.             case WM_MOUSEMOVE:
  47.             // If we pass this on we don't get any mouse clicks
  48.             //case WM_NCHITTEST:
  49.             case WM_NCLBUTTONDBLCLK:
  50.             case WM_NCLBUTTONDOWN:
  51.             case WM_NCLBUTTONUP:
  52.             case WM_NCMBUTTONDBLCLK:
  53.             case WM_NCMBUTTONDOWN:
  54.             case WM_NCMBUTTONUP:
  55.             case WM_NCMOUSEMOVE:
  56.             case WM_NCRBUTTONDBLCLK:
  57.             case WM_NCRBUTTONDOWN:
  58.             case WM_NCRBUTTONUP:
  59.             case WM_RBUTTONDBLCLK:
  60.             case WM_RBUTTONDOWN:
  61.             case WM_RBUTTONUP:
  62.             case WM_SYSCHAR:
  63.             case WM_SYSDEADCHAR:
  64.             case WM_SYSKEYDOWN:
  65.             case WM_SYSKEYUP:
  66.  
  67.                 DbgLog((LOG_TRACE, 2, TEXT("Forwarding %x to drain")));
  68.                 PostMessage(hwndDrain, uMsg, wParam, lParam);
  69.  
  70.                 return TRUE;
  71.         }
  72.     }
  73.     return FALSE;
  74. }
  75.  
  76.  
  77. // This class implements the IVideoWindow control functions (dual interface)
  78. // we support a large number of properties and methods designed to allow the
  79. // client (whether it be an automation controller or a C/C++ application) to
  80. // set and get a number of window related properties such as it's position.
  81. // We also support some methods that duplicate the properties but provide a
  82. // more direct and efficient mechanism as many values may be changed in one
  83.  
  84. CBaseControlWindow::CBaseControlWindow(
  85.                         CBaseFilter *pFilter,        // Owning filter
  86.                         CCritSec *pInterfaceLock,    // Locking object
  87.                         TCHAR *pName,                // Object description
  88.                         LPUNKNOWN pUnk,              // Normal COM ownership
  89.                         HRESULT *phr) :              // OLE return code
  90.  
  91.     CBaseVideoWindow(pName,pUnk),
  92.     m_pInterfaceLock(pInterfaceLock),
  93.     m_hwndOwner(NULL),
  94.     m_hwndDrain(NULL),
  95.     m_bAutoShow(TRUE),
  96.     m_pFilter(pFilter),
  97.     m_bCursorHidden(FALSE),
  98.     m_pPin(NULL)
  99. {
  100.     ASSERT(m_pFilter);
  101.     ASSERT(m_pInterfaceLock);
  102.     ASSERT(phr);
  103.     m_BorderColour = VIDEO_COLOUR;
  104. }
  105.  
  106.  
  107. // Set the title caption on the base window, we don't do any field checking
  108. // as we really don't care what title they intend to have. We can always get
  109. // it back again later with GetWindowText. The only other complication is to
  110. // do the necessary string conversions between ANSI and OLE Unicode strings
  111.  
  112. STDMETHODIMP CBaseControlWindow::put_Caption(BSTR strCaption)
  113. {
  114.     CheckPointer(strCaption,E_POINTER);
  115.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  116. #ifdef UNICODE
  117.     SetWindowText(m_hwnd, strCaption);
  118. #else
  119.     CHAR Caption[CAPTION];
  120.  
  121.     WideCharToMultiByte(CP_ACP,0,strCaption,-1,Caption,CAPTION,NULL,NULL);
  122.     SetWindowText(m_hwnd, Caption);
  123. #endif
  124.     return NOERROR;
  125. }
  126.  
  127.  
  128. // Get the current base window title caption, once again we do no real field
  129. // checking. We allocate a string for the window title to be filled in with
  130. // which ensures the interface doesn't fiddle around with getting memory. A
  131. // BSTR is a normal C string with the length at position (-1), we use the
  132. // WriteBSTR helper function to create the caption to try and avoid OLE32
  133.  
  134. STDMETHODIMP CBaseControlWindow::get_Caption(BSTR *pstrCaption)
  135. {
  136.     CheckPointer(pstrCaption,E_POINTER);
  137.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  138.     WCHAR WideCaption[CAPTION];
  139.  
  140. #ifdef UNICODE
  141.     GetWindowText(m_hwnd,WideCaption,CAPTION);
  142. #else
  143.     // Convert the ASCII caption to a UNICODE string
  144.  
  145.     TCHAR Caption[CAPTION];
  146.     GetWindowText(m_hwnd,Caption,CAPTION);
  147.     MultiByteToWideChar(CP_ACP,0,Caption,-1,WideCaption,CAPTION);
  148. #endif
  149.     return WriteBSTR(pstrCaption,WideCaption);
  150. }
  151.  
  152.  
  153. // Set the window style using GWL_EXSTYLE
  154.  
  155. STDMETHODIMP CBaseControlWindow::put_WindowStyleEx(long WindowStyleEx)
  156. {
  157.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  158.  
  159.     // Should we be taking off WS_EX_TOPMOST
  160.  
  161.     if (GetWindowLong(m_hwnd,GWL_EXSTYLE) & WS_EX_TOPMOST) {
  162.         if ((WindowStyleEx & WS_EX_TOPMOST) == 0) {
  163.             SendMessage(m_hwnd,m_ShowStageTop,(WPARAM) FALSE,(LPARAM) 0);
  164.         }
  165.     }
  166.  
  167.     // Likewise should we be adding WS_EX_TOPMOST
  168.  
  169.     if (WindowStyleEx & WS_EX_TOPMOST) {
  170.         SendMessage(m_hwnd,m_ShowStageTop,(WPARAM) TRUE,(LPARAM) 0);
  171.         WindowStyleEx &= (~WS_EX_TOPMOST);
  172.         if (WindowStyleEx == 0) return NOERROR;
  173.     }
  174.     return DoSetWindowStyle(WindowStyleEx,GWL_EXSTYLE);
  175. }
  176.  
  177.  
  178. // Gets the current GWL_EXSTYLE base window style
  179.  
  180. STDMETHODIMP CBaseControlWindow::get_WindowStyleEx(long *pWindowStyleEx)
  181. {
  182.     CheckPointer(pWindowStyleEx,E_POINTER);
  183.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  184.     return DoGetWindowStyle(pWindowStyleEx,GWL_EXSTYLE);
  185. }
  186.  
  187.  
  188. // Set the window style using GWL_STYLE
  189.  
  190. STDMETHODIMP CBaseControlWindow::put_WindowStyle(long WindowStyle)
  191. {
  192.     // These styles cannot be changed dynamically
  193.  
  194.     if ((WindowStyle & WS_DISABLED) ||
  195.         (WindowStyle & WS_ICONIC) ||
  196.         (WindowStyle & WS_MAXIMIZE) ||
  197.         (WindowStyle & WS_MINIMIZE) ||
  198.         (WindowStyle & WS_HSCROLL) ||
  199.         (WindowStyle & WS_VSCROLL)) {
  200.  
  201.             return E_INVALIDARG;
  202.     }
  203.  
  204.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  205.     return DoSetWindowStyle(WindowStyle,GWL_STYLE);
  206. }
  207.  
  208.  
  209. // Get the current GWL_STYLE base window style
  210.  
  211. STDMETHODIMP CBaseControlWindow::get_WindowStyle(long *pWindowStyle)
  212. {
  213.     CheckPointer(pWindowStyle,E_POINTER);
  214.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  215.     return DoGetWindowStyle(pWindowStyle,GWL_STYLE);
  216. }
  217.  
  218.  
  219. // Change the base window style or the extended styles depending on whether
  220. // WindowLong is GWL_STYLE or GWL_EXSTYLE. We must call SetWindowPos to have
  221. // the window displayed in it's new style after the change which is a little
  222. // tricky if the window is not currently visible as we realise it offscreen.
  223. // In most cases the client will call get_WindowStyle before they call this
  224. // and then AND and OR in extra bit settings according to the requirements
  225.  
  226. HRESULT CBaseControlWindow::DoSetWindowStyle(long Style,long WindowLong)
  227. {
  228.     RECT WindowRect;
  229.  
  230.     // Get the window's visibility before setting the style
  231.     BOOL bVisible = IsWindowVisible(m_hwnd);
  232.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  233.  
  234.     // Set the new style flags for the window
  235.     SetWindowLong(m_hwnd,WindowLong,Style);
  236.     UINT WindowFlags = SWP_SHOWWINDOW | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  237.     WindowFlags |= SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE;
  238.  
  239.     // Show the window again in the current position
  240.  
  241.     if (bVisible == TRUE) {
  242.  
  243.         SetWindowPos(m_hwnd,            // Base window handle
  244.                      HWND_TOP,          // Just a place holder
  245.                      0,0,0,0,           // Leave size and position
  246.                      WindowFlags);      // Just draw it again
  247.  
  248.         return NOERROR;
  249.     }
  250.  
  251.     // Move the window offscreen so the user doesn't see the changes
  252.  
  253.     MoveWindow((HWND) m_hwnd,                     // Base window handle
  254.                GetSystemMetrics(SM_CXSCREEN),     // Current desktop width
  255.                GetSystemMetrics(SM_CYSCREEN),     // Likewise it's height
  256.                WIDTH(&WindowRect),                // Use the same width
  257.                HEIGHT(&WindowRect),               // Keep height same to
  258.                TRUE);                             // May as well repaint
  259.  
  260.     // Now show the previously hidden window
  261.  
  262.     SetWindowPos(m_hwnd,            // Base window handle
  263.                  HWND_TOP,          // Just a place holder
  264.                  0,0,0,0,           // Leave size and position
  265.                  WindowFlags);      // Just draw it again
  266.  
  267.     EXECUTE_ASSERT(ShowWindow(m_hwnd,SW_HIDE));
  268.  
  269.     if (GetParent(m_hwnd)) {
  270.  
  271.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  272.     }
  273.  
  274.     MoveWindow((HWND) m_hwnd,        // Base window handle
  275.                WindowRect.left,      // Existing x coordinate
  276.                WindowRect.top,       // Existing y coordinate
  277.                WIDTH(&WindowRect),   // Use the same width
  278.                HEIGHT(&WindowRect),  // Keep height same to
  279.                TRUE);                // May as well repaint
  280.  
  281.     return NOERROR;
  282. }
  283.  
  284.  
  285. // Get the current base window style (either GWL_STYLE or GWL_EXSTYLE)
  286.  
  287. HRESULT CBaseControlWindow::DoGetWindowStyle(long *pStyle,long WindowLong)
  288. {
  289.     *pStyle = GetWindowLong(m_hwnd,WindowLong);
  290.     return NOERROR;
  291. }
  292.  
  293.  
  294. // Change the visibility of the base window, this takes the same parameters
  295. // as the ShowWindow Win32 API does, so the client can have the window hidden
  296. // or shown, minimised to an icon, or maximised to play in full screen mode
  297. // We pass the request on to the base window to actually make the change
  298.  
  299. STDMETHODIMP CBaseControlWindow::put_WindowState(long WindowState)
  300. {
  301.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  302.     DoShowWindow(WindowState);
  303.     return NOERROR;
  304. }
  305.  
  306.  
  307. // Get the current window state, this function returns a subset of the SW bit
  308. // settings available in ShowWindow, if the window is visible then SW_SHOW is
  309. // set, if it is hidden then the SW_HIDDEN is set, if it is either minimised
  310. // or maximised then the SW_MINIMIZE or SW_MAXIMIZE is set respectively. The
  311. // other SW bit settings are really set commands not readable output values
  312.  
  313. STDMETHODIMP CBaseControlWindow::get_WindowState(long *pWindowState)
  314. {
  315.     CheckPointer(pWindowState,E_POINTER);
  316.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  317.     ASSERT(pWindowState);
  318.     *pWindowState = FALSE;
  319.  
  320.     // Is the window visible, a window is termed visible if it is somewhere on
  321.     // the current desktop even if it is completely obscured by other windows
  322.     // so the flag is a style for each window set with the WS_VISIBLE bit
  323.  
  324.     if (IsWindowVisible(m_hwnd) == TRUE) {
  325.  
  326.         // Is the base window iconic
  327.         if (IsIconic(m_hwnd) == TRUE) {
  328.             *pWindowState |= SW_MINIMIZE;
  329.         }
  330.  
  331.         // Has the window been maximised
  332.         else if (IsZoomed(m_hwnd) == TRUE) {
  333.             *pWindowState |= SW_MAXIMIZE;
  334.         }
  335.  
  336.         // Window is normal
  337.         else {
  338.             *pWindowState |= SW_SHOW;
  339.         }
  340.  
  341.     } else {
  342.         *pWindowState |= SW_HIDE;
  343.     }
  344.     return NOERROR;
  345. }
  346.  
  347.  
  348. // This makes sure that any palette we realise in the base window (through a
  349. // media type or through the overlay interface) is done in the background and
  350. // is therefore mapped to existing device entries rather than taking it over
  351. // as it will do when we this window gets the keyboard focus. An application
  352. // uses this to make sure it doesn't have it's palette removed by the window
  353.  
  354. STDMETHODIMP CBaseControlWindow::put_BackgroundPalette(long BackgroundPalette)
  355. {
  356.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  357.     CAutoLock cWindowLock(&m_WindowLock);
  358.  
  359.     // Check this is a valid automation boolean type
  360.  
  361.     if (BackgroundPalette != OATRUE) {
  362.         if (BackgroundPalette != OAFALSE) {
  363.             return E_INVALIDARG;
  364.         }
  365.     }
  366.  
  367.     // Make sure the window realises any palette it has again
  368.  
  369.     m_bBackground = (BackgroundPalette == OATRUE ? TRUE : FALSE);
  370.     PostMessage(m_hwnd,m_RealizePalette,0,0);
  371.     PaintWindow(FALSE);
  372.  
  373.     return NOERROR;
  374. }
  375.  
  376.  
  377. // This returns the current background realisation setting
  378.  
  379. STDMETHODIMP
  380. CBaseControlWindow::get_BackgroundPalette(long *pBackgroundPalette)
  381. {
  382.     CheckPointer(pBackgroundPalette,E_POINTER);
  383.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  384.     CAutoLock cWindowLock(&m_WindowLock);
  385.  
  386.     // Get the current background palette setting
  387.  
  388.     *pBackgroundPalette = (m_bBackground == TRUE ? OATRUE : OAFALSE);
  389.     return NOERROR;
  390. }
  391.  
  392.  
  393. // Change the visibility of the base window
  394.  
  395. STDMETHODIMP CBaseControlWindow::put_Visible(long Visible)
  396. {
  397.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  398.  
  399.     // Check this is a valid automation boolean type
  400.  
  401.     if (Visible != OATRUE) {
  402.         if (Visible != OAFALSE) {
  403.             return E_INVALIDARG;
  404.         }
  405.     }
  406.  
  407.     // Convert the boolean visibility into SW_SHOW and SW_HIDE
  408.  
  409.     INT Mode = (Visible == OATRUE ? SW_SHOWNORMAL : SW_HIDE);
  410.     DoShowWindow(Mode);
  411.     return NOERROR;
  412. }
  413.  
  414.  
  415. // Return OATRUE if the window is currently visible otherwise OAFALSE
  416.  
  417. STDMETHODIMP CBaseControlWindow::get_Visible(long *pVisible)
  418. {
  419.     CheckPointer(pVisible,E_POINTER);
  420.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  421.  
  422.     // See if the base window has a WS_VISIBLE style - this will return TRUE
  423.     // even if the window is completely obscured by other desktop windows, we
  424.     // return FALSE if the window is not showing because of earlier calls
  425.  
  426.     BOOL Mode = IsWindowVisible(m_hwnd);
  427.     *pVisible = (Mode == TRUE ? OATRUE : OAFALSE);
  428.     return NOERROR;
  429. }
  430.  
  431.  
  432. // Change the left position of the base window. This keeps the window width
  433. // and height properties the same so it effectively shunts the window left or
  434. // right accordingly - there is the Width property to change that dimension
  435.  
  436. STDMETHODIMP CBaseControlWindow::put_Left(long Left)
  437. {
  438.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  439.     BOOL bSuccess;
  440.     RECT WindowRect;
  441.  
  442.     // Get the current window position in a RECT
  443.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  444.  
  445.     if (GetParent(m_hwnd)) {
  446.  
  447.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  448.     }
  449.  
  450.     // Adjust the coordinates ready for SetWindowPos, the window rectangle we
  451.     // get back from GetWindowRect is in left,top,right and bottom while the
  452.     // coordinates SetWindowPos wants are left,top,width and height values
  453.  
  454.     WindowRect.bottom = WindowRect.bottom - WindowRect.top;
  455.     WindowRect.right = WindowRect.right - WindowRect.left;
  456.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  457.  
  458.     bSuccess = SetWindowPos(m_hwnd,                // Window handle
  459.                             HWND_TOP,              // Put it at the top
  460.                             Left,                  // New left position
  461.                             WindowRect.top,        // Leave top alone
  462.                             WindowRect.right,      // The WIDTH (not right)
  463.                             WindowRect.bottom,     // The HEIGHT (not bottom)
  464.                             WindowFlags);          // Show window options
  465.  
  466.     if (bSuccess == FALSE) {
  467.         return E_INVALIDARG;
  468.     }
  469.     return NOERROR;
  470. }
  471.  
  472.  
  473. // Return the current base window left position
  474.  
  475. STDMETHODIMP CBaseControlWindow::get_Left(long *pLeft)
  476. {
  477.     CheckPointer(pLeft,E_POINTER);
  478.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  479.     RECT WindowRect;
  480.  
  481.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  482.     *pLeft = WindowRect.left;
  483.     return NOERROR;
  484. }
  485.  
  486.  
  487. // Change the current width of the base window. This property complements the
  488. // left position property so we must keep the left edge constant and expand or
  489. // contract to the right, the alternative would be to change the left edge so
  490. // keeping the right edge constant but this is maybe a little more intuitive
  491.  
  492. STDMETHODIMP CBaseControlWindow::put_Width(long Width)
  493. {
  494.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  495.     BOOL bSuccess;
  496.     RECT WindowRect;
  497.  
  498.     // Adjust the coordinates ready for SetWindowPos, the window rectangle we
  499.     // get back from GetWindowRect is in left,top,right and bottom while the
  500.     // coordinates SetWindowPos wants are left,top,width and height values
  501.  
  502.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  503.  
  504.     if (GetParent(m_hwnd)) {
  505.  
  506.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  507.     }
  508.  
  509.     WindowRect.bottom = WindowRect.bottom - WindowRect.top;
  510.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  511.  
  512.     // This seems to have a bug in that calling SetWindowPos on a window with
  513.     // just the width changing causes it to ignore the width that you pass in
  514.     // and sets it to a mimimum value of 110 pixels wide (Windows NT 3.51)
  515.  
  516.     bSuccess = SetWindowPos(m_hwnd,                // Window handle
  517.                             HWND_TOP,              // Put it at the top
  518.                             WindowRect.left,       // Leave left alone
  519.                             WindowRect.top,        // Leave top alone
  520.                             Width,                 // New WIDTH dimension
  521.                             WindowRect.bottom,     // The HEIGHT (not bottom)
  522.                             WindowFlags);          // Show window options
  523.  
  524.     if (bSuccess == FALSE) {
  525.         return E_INVALIDARG;
  526.     }
  527.     return NOERROR;
  528. }
  529.  
  530.  
  531. // Return the current base window width
  532.  
  533. STDMETHODIMP CBaseControlWindow::get_Width(long *pWidth)
  534. {
  535.     CheckPointer(pWidth,E_POINTER);
  536.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  537.     RECT WindowRect;
  538.  
  539.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  540.     *pWidth = WindowRect.right - WindowRect.left;
  541.     return NOERROR;
  542. }
  543.  
  544.  
  545. // This allows the client program to change the top position for the window in
  546. // the same way that changing the left position does not affect the width of
  547. // the image so changing the top position does not affect the window height
  548.  
  549. STDMETHODIMP CBaseControlWindow::put_Top(long Top)
  550. {
  551.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  552.     BOOL bSuccess;
  553.     RECT WindowRect;
  554.  
  555.     // Get the current window position in a RECT
  556.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  557.  
  558.     if (GetParent(m_hwnd)) {
  559.  
  560.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  561.     }
  562.  
  563.     // Adjust the coordinates ready for SetWindowPos, the window rectangle we
  564.     // get back from GetWindowRect is in left,top,right and bottom while the
  565.     // coordinates SetWindowPos wants are left,top,width and height values
  566.  
  567.     WindowRect.bottom = WindowRect.bottom - WindowRect.top;
  568.     WindowRect.right = WindowRect.right - WindowRect.left;
  569.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  570.  
  571.     bSuccess = SetWindowPos(m_hwnd,                // Window handle
  572.                             HWND_TOP,              // Put it at the top
  573.                             WindowRect.left,       // Leave left alone
  574.                             Top,                   // New top position
  575.                             WindowRect.right,      // The WIDTH (not right)
  576.                             WindowRect.bottom,     // The HEIGHT (not bottom)
  577.                             WindowFlags);          // Show window flags
  578.  
  579.     if (bSuccess == FALSE) {
  580.         return E_INVALIDARG;
  581.     }
  582.     return NOERROR;
  583. }
  584.  
  585.  
  586. // Return the current base window top position
  587.  
  588. STDMETHODIMP CBaseControlWindow::get_Top(long *pTop)
  589. {
  590.     CheckPointer(pTop,E_POINTER);
  591.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  592.     RECT WindowRect;
  593.  
  594.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  595.     *pTop = WindowRect.top;
  596.     return NOERROR;
  597. }
  598.  
  599.  
  600. // Change the height of the window, this complements the top property so when
  601. // we change this we must keep the top position for the base window, as said
  602. // before we could keep the bottom and grow upwards although this is perhaps
  603. // a little more intuitive since we already have a top position property
  604.  
  605. STDMETHODIMP CBaseControlWindow::put_Height(long Height)
  606. {
  607.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  608.     BOOL bSuccess;
  609.     RECT WindowRect;
  610.  
  611.     // Adjust the coordinates ready for SetWindowPos, the window rectangle we
  612.     // get back from GetWindowRect is in left,top,right and bottom while the
  613.     // coordinates SetWindowPos wants are left,top,width and height values
  614.  
  615.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  616.  
  617.     if (GetParent(m_hwnd)) {
  618.  
  619.         MapWindowPoints(HWND_DESKTOP, GetParent(m_hwnd), (LPPOINT)&WindowRect, 2);
  620.     }
  621.  
  622.     WindowRect.right = WindowRect.right - WindowRect.left;
  623.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  624.  
  625.     bSuccess = SetWindowPos(m_hwnd,                // Window handle
  626.                             HWND_TOP,              // Put it at the top
  627.                             WindowRect.left,       // Leave left alone
  628.                             WindowRect.top,        // Leave top alone
  629.                             WindowRect.right,      // The WIDTH (not right)
  630.                             Height,                // New height dimension
  631.                             WindowFlags);          // Show window flags
  632.  
  633.     if (bSuccess == FALSE) {
  634.         return E_INVALIDARG;
  635.     }
  636.     return NOERROR;
  637. }
  638.  
  639.  
  640. // Return the current base window height
  641.  
  642. STDMETHODIMP CBaseControlWindow::get_Height(long *pHeight)
  643. {
  644.     CheckPointer(pHeight,E_POINTER);
  645.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  646.     RECT WindowRect;
  647.  
  648.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  649.     *pHeight = WindowRect.bottom - WindowRect.top;
  650.     return NOERROR;
  651. }
  652.  
  653.  
  654. // This can be called to change the owning window. Setting the owner is done
  655. // through this function, however to make the window a true child window the
  656. // style must also be set to WS_CHILD. After resetting the owner to NULL an
  657. // application should also set the style to WS_OVERLAPPED | WS_CLIPCHILDREN.
  658.  
  659. // We cannot lock the object here because the SetParent causes an interthread
  660. // SendMessage to the owner window. If they are in GetState we will sit here
  661. // incomplete with the critical section locked therefore blocking out source
  662. // filter threads from accessing us. Because the source thread can't enter us
  663. // it can't get buffers or call EndOfStream so the GetState will not complete
  664.  
  665. STDMETHODIMP CBaseControlWindow::put_Owner(OAHWND Owner)
  666. {
  667.     // Check we are connected otherwise reject the call
  668.  
  669.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  670.     m_hwndOwner = (HWND) Owner;
  671.     HWND hwndParent = m_hwndOwner;
  672.  
  673.     // Add or remove WS_CHILD as appropriate
  674.  
  675.     LONG Style = GetWindowLong(m_hwnd,GWL_STYLE);
  676.     if (Owner == NULL) {
  677.         Style &= (~WS_CHILD);
  678.     } else {
  679.         Style |= (WS_CHILD);
  680.     }
  681.     SetWindowLong(m_hwnd,GWL_STYLE,Style);
  682.  
  683.     // Don't call this with the filter locked
  684.  
  685.     SetParent(m_hwnd,hwndParent);
  686.  
  687.     PaintWindow(TRUE);
  688.     NOTE1("Changed parent %lx",hwndParent);
  689.  
  690.     return NOERROR;
  691. }
  692.  
  693.  
  694. // This complements the put_Owner to get the current owning window property
  695. // we always return NOERROR although the returned window handle may be NULL
  696. // to indicate no owning window (the desktop window doesn't qualify as one)
  697. // If an application sets the owner we call SetParent, however that returns
  698. // NULL until the WS_CHILD bit is set on, so we store the owner internally
  699.  
  700. STDMETHODIMP CBaseControlWindow::get_Owner(OAHWND *Owner)
  701. {
  702.     CheckPointer(Owner,E_POINTER);
  703.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  704.     *Owner = (OAHWND) m_hwndOwner;
  705.     return NOERROR;
  706. }
  707.  
  708.  
  709. // And renderer supporting IVideoWindow may have an HWND set who will get any
  710. // keyboard and mouse messages we receive posted on to them. This is separate
  711. // from setting an owning window. By separating the two, applications may get
  712. // messages sent on even when they have set no owner (perhaps it's maximised)
  713.  
  714. STDMETHODIMP CBaseControlWindow::put_MessageDrain(OAHWND Drain)
  715. {
  716.     // Check we are connected otherwise reject the call
  717.  
  718.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  719.     m_hwndDrain = (HWND) Drain;
  720.     return NOERROR;
  721. }
  722.  
  723.  
  724. // Return the current message drain
  725.  
  726. STDMETHODIMP CBaseControlWindow::get_MessageDrain(OAHWND *Drain)
  727. {
  728.     CheckPointer(Drain,E_POINTER);
  729.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  730.     *Drain = (OAHWND) m_hwndDrain;
  731.     return NOERROR;
  732. }
  733.  
  734.  
  735. // This is called by the filter graph to inform us of a message we should know
  736. // is being sent to our owning window. We have this because as a child window
  737. // we do not get certain messages that are only sent to top level windows. We
  738. // must see the palette changed/changing/query messages so that we know if we
  739. // have the foreground palette or not. We pass the message on to our window
  740. // using SendMessage - this will cause an interthread send message to occur
  741.  
  742. STDMETHODIMP
  743. CBaseControlWindow::NotifyOwnerMessage(OAHWND hwnd,    // Window handle
  744.                                        long uMsg,    // Message ID
  745.                                        LONG_PTR wParam,  // Parameters
  746.                                        LONG_PTR lParam)  // for message
  747. {
  748.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  749.  
  750.     // Only interested in these Windows messages
  751.  
  752.     switch (uMsg) {
  753.  
  754.         case WM_SYSCOLORCHANGE:
  755.         case WM_PALETTECHANGED:
  756.         case WM_PALETTEISCHANGING:
  757.         case WM_QUERYNEWPALETTE:
  758.         case WM_DEVMODECHANGE:
  759.         case WM_DISPLAYCHANGE:
  760.         case WM_ACTIVATEAPP:
  761.  
  762.             // If we do not have an owner then ignore
  763.  
  764.             if (m_hwndOwner == NULL) {
  765.                 return NOERROR;
  766.             }
  767.             SendMessage(m_hwnd,uMsg,(WPARAM)wParam,(LPARAM)lParam);
  768.         break;
  769.  
  770.     // do NOT fwd WM_MOVE. the parameters are the location of the parent
  771.     // window, NOT what the renderer should be looking at.  But we need
  772.     // to make sure the overlay is moved with the parent window, so we
  773.     // do this.
  774.     case WM_MOVE:
  775.         PostMessage(m_hwnd,WM_PAINT,0,0);
  776.         break;
  777.     }
  778.     return NOERROR;
  779. }
  780.  
  781.  
  782. // Allow an application to have us set the base window in the foreground. We
  783. // have this because it is difficult for one thread to do do this to a window
  784. // owned by another thread. We ask the base window class to do the real work
  785.  
  786. STDMETHODIMP CBaseControlWindow::SetWindowForeground(long Focus)
  787. {
  788.     // Check this is a valid automation boolean type
  789.  
  790.     if (Focus != OATRUE) {
  791.         if (Focus != OAFALSE) {
  792.             return E_INVALIDARG;
  793.         }
  794.     }
  795.  
  796.     // We shouldn't lock as this sends a message
  797.  
  798.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  799.     BOOL bFocus = (Focus == OATRUE ? TRUE : FALSE);
  800.     DoSetWindowForeground(bFocus);
  801.  
  802.     return NOERROR;
  803. }
  804.  
  805.  
  806. // This allows a client to set the complete window size and position in one
  807. // atomic operation. The same affect can be had by changing each dimension
  808. // in turn through their individual properties although some flashing will
  809. // occur as each of them gets updated (they are better set at design time)
  810.  
  811. STDMETHODIMP
  812. CBaseControlWindow::SetWindowPosition(long Left,long Top,long Width,long Height)
  813. {
  814.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  815.     BOOL bSuccess;
  816.  
  817.     // Set the new size and position
  818.     UINT WindowFlags = SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOACTIVATE;
  819.  
  820.     ASSERT(IsWindow(m_hwnd));
  821.     bSuccess = SetWindowPos(m_hwnd,         // Window handle
  822.                             HWND_TOP,       // Put it at the top
  823.                             Left,           // Left position
  824.                             Top,            // Top position
  825.                             Width,          // Window width
  826.                             Height,         // Window height
  827.                             WindowFlags);   // Show window flags
  828.     ASSERT(bSuccess);
  829. #ifdef DEBUG
  830.     DbgLog((LOG_TRACE, 1, TEXT("SWP failed error %d"), GetLastError()));
  831. #endif
  832.     if (bSuccess == FALSE) {
  833.         return E_INVALIDARG;
  834.     }
  835.     return NOERROR;
  836. }
  837.  
  838.  
  839. // This complements the SetWindowPosition to return the current window place
  840. // in device coordinates. As before the same information can be retrived by
  841. // calling the property get functions individually but this is atomic and is
  842. // therefore more suitable to a live environment rather than design time
  843.  
  844. STDMETHODIMP
  845. CBaseControlWindow::GetWindowPosition(long *pLeft,long *pTop,long *pWidth,long *pHeight)
  846. {
  847.     // Should check the pointers are not NULL
  848.  
  849.     CheckPointer(pLeft,E_POINTER);
  850.     CheckPointer(pTop,E_POINTER);
  851.     CheckPointer(pWidth,E_POINTER);
  852.     CheckPointer(pHeight,E_POINTER);
  853.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  854.     RECT WindowRect;
  855.  
  856.     // Get the current window coordinates
  857.  
  858.     EXECUTE_ASSERT(GetWindowRect(m_hwnd,&WindowRect));
  859.  
  860.     // Convert the RECT into left,top,width and height values
  861.  
  862.     *pLeft = WindowRect.left;
  863.     *pTop = WindowRect.top;
  864.     *pWidth = WindowRect.right - WindowRect.left;
  865.     *pHeight = WindowRect.bottom - WindowRect.top;
  866.  
  867.     return NOERROR;
  868. }
  869.  
  870.  
  871. // When a window is maximised or iconic calling GetWindowPosition will return
  872. // the current window position (likewise for the properties). However if the
  873. // restored size (ie the size we'll return to when normally shown) is needed
  874. // then this should be used. When in a normal position (neither iconic nor
  875. // maximised) then this returns the same coordinates as GetWindowPosition
  876.  
  877. STDMETHODIMP
  878. CBaseControlWindow::GetRestorePosition(long *pLeft,long *pTop,long *pWidth,long *pHeight)
  879. {
  880.     // Should check the pointers are not NULL
  881.  
  882.     CheckPointer(pLeft,E_POINTER);
  883.     CheckPointer(pTop,E_POINTER);
  884.     CheckPointer(pWidth,E_POINTER);
  885.     CheckPointer(pHeight,E_POINTER);
  886.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  887.  
  888.     // Use GetWindowPlacement to find the restore position
  889.  
  890.     WINDOWPLACEMENT Place;
  891.     Place.length = sizeof(WINDOWPLACEMENT);
  892.     EXECUTE_ASSERT(GetWindowPlacement(m_hwnd,&Place));
  893.  
  894.     RECT WorkArea;
  895.  
  896.     // We must take into account any task bar present
  897.  
  898.     if (SystemParametersInfo(SPI_GETWORKAREA,0,&WorkArea,FALSE) == TRUE) {
  899.         if (GetParent(m_hwnd) == NULL) {
  900.             Place.rcNormalPosition.top += WorkArea.top;
  901.             Place.rcNormalPosition.bottom += WorkArea.top;
  902.             Place.rcNormalPosition.left += WorkArea.left;
  903.             Place.rcNormalPosition.right += WorkArea.left;
  904.         }
  905.     }
  906.  
  907.     // Convert the RECT into left,top,width and height values
  908.  
  909.     *pLeft = Place.rcNormalPosition.left;
  910.     *pTop = Place.rcNormalPosition.top;
  911.     *pWidth = Place.rcNormalPosition.right - Place.rcNormalPosition.left;
  912.     *pHeight = Place.rcNormalPosition.bottom - Place.rcNormalPosition.top;
  913.  
  914.     return NOERROR;
  915. }
  916.  
  917.  
  918. // Return the current border colour, if we are playing something to a subset
  919. // of the base window display there is an outside area exposed. The default
  920. // action is to paint this colour in the Windows background colour (defined
  921. // as value COLOR_WINDOW) We reset to this default when we're disconnected
  922.  
  923. STDMETHODIMP CBaseControlWindow::get_BorderColor(long *Color)
  924. {
  925.     CheckPointer(Color,E_POINTER);
  926.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  927.     *Color = (long) m_BorderColour;
  928.     return NOERROR;
  929. }
  930.  
  931.  
  932. // This can be called to set the current border colour
  933.  
  934. STDMETHODIMP CBaseControlWindow::put_BorderColor(long Color)
  935. {
  936.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  937.  
  938.     // Have the window repainted with the new border colour
  939.  
  940.     m_BorderColour = (COLORREF) Color;
  941.     PaintWindow(TRUE);
  942.     return NOERROR;
  943. }
  944.  
  945.  
  946. // Delegate fullscreen handling to plug in distributor
  947.  
  948. STDMETHODIMP CBaseControlWindow::get_FullScreenMode(long *FullScreenMode)
  949. {
  950.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  951.     CheckPointer(FullScreenMode,E_POINTER);
  952.     return E_NOTIMPL;
  953. }
  954.  
  955.  
  956. // Delegate fullscreen handling to plug in distributor
  957.  
  958. STDMETHODIMP CBaseControlWindow::put_FullScreenMode(long FullScreenMode)
  959. {
  960.     return E_NOTIMPL;
  961. }
  962.  
  963.  
  964. // This sets the auto show property, this property causes the base window to
  965. // be displayed whenever we change state. This allows an application to have
  966. // to do nothing to have the window appear but still allow them to change the
  967. // default behaviour if for example they want to keep it hidden for longer
  968.  
  969. STDMETHODIMP CBaseControlWindow::put_AutoShow(long AutoShow)
  970. {
  971.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  972.  
  973.     // Check this is a valid automation boolean type
  974.  
  975.     if (AutoShow != OATRUE) {
  976.         if (AutoShow != OAFALSE) {
  977.             return E_INVALIDARG;
  978.         }
  979.     }
  980.  
  981.     m_bAutoShow = (AutoShow == OATRUE ? TRUE : FALSE);
  982.     return NOERROR;
  983. }
  984.  
  985.  
  986. // This can be called to get the current auto show flag. The flag is updated
  987. // when we connect and disconnect and through this interface all of which are
  988. // controlled and serialised by means of the main renderer critical section
  989.  
  990. STDMETHODIMP CBaseControlWindow::get_AutoShow(long *AutoShow)
  991. {
  992.     CheckPointer(AutoShow,E_POINTER);
  993.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  994.     *AutoShow = (m_bAutoShow == TRUE ? OATRUE : OAFALSE);
  995.     return NOERROR;
  996. }
  997.  
  998.  
  999. // Return the minimum ideal image size for the current video. This may differ
  1000. // to the actual video dimensions because we may be using DirectDraw hardware
  1001. // that has specific stretching requirements. For example the Cirrus Logic
  1002. // cards have a minimum stretch factor depending on the overlay surface size
  1003.  
  1004. STDMETHODIMP
  1005. CBaseControlWindow::GetMinIdealImageSize(long *pWidth,long *pHeight)
  1006. {
  1007.     CheckPointer(pWidth,E_POINTER);
  1008.     CheckPointer(pHeight,E_POINTER);
  1009.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1010.     FILTER_STATE State;
  1011.  
  1012.     // Must not be stopped for this to work correctly
  1013.  
  1014.     m_pFilter->GetState(0,&State);
  1015.     if (State == State_Stopped) {
  1016.         return VFW_E_WRONG_STATE;
  1017.     }
  1018.  
  1019.     RECT DefaultRect = GetDefaultRect();
  1020.     *pWidth = WIDTH(&DefaultRect);
  1021.     *pHeight = HEIGHT(&DefaultRect);
  1022.     return NOERROR;
  1023. }
  1024.  
  1025.  
  1026. // Return the maximum ideal image size for the current video. This may differ
  1027. // to the actual video dimensions because we may be using DirectDraw hardware
  1028. // that has specific stretching requirements. For example the Cirrus Logic
  1029. // cards have a maximum stretch factor depending on the overlay surface size
  1030.  
  1031. STDMETHODIMP
  1032. CBaseControlWindow::GetMaxIdealImageSize(long *pWidth,long *pHeight)
  1033. {
  1034.     CheckPointer(pWidth,E_POINTER);
  1035.     CheckPointer(pHeight,E_POINTER);
  1036.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1037.     FILTER_STATE State;
  1038.  
  1039.     // Must not be stopped for this to work correctly
  1040.  
  1041.     m_pFilter->GetState(0,&State);
  1042.     if (State == State_Stopped) {
  1043.         return VFW_E_WRONG_STATE;
  1044.     }
  1045.  
  1046.     RECT DefaultRect = GetDefaultRect();
  1047.     *pWidth = WIDTH(&DefaultRect);
  1048.     *pHeight = HEIGHT(&DefaultRect);
  1049.     return NOERROR;
  1050. }
  1051.  
  1052.  
  1053. // Allow an application to hide the cursor on our window
  1054.  
  1055. STDMETHODIMP
  1056. CBaseControlWindow::HideCursor(long HideCursor)
  1057. {
  1058.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1059.  
  1060.     // Check this is a valid automation boolean type
  1061.  
  1062.     if (HideCursor != OATRUE) {
  1063.         if (HideCursor != OAFALSE) {
  1064.             return E_INVALIDARG;
  1065.         }
  1066.     }
  1067.  
  1068.     m_bCursorHidden = (HideCursor == OATRUE ? TRUE : FALSE);
  1069.     return NOERROR;
  1070. }
  1071.  
  1072.  
  1073. // Returns whether we have the cursor hidden or not
  1074.  
  1075. STDMETHODIMP CBaseControlWindow::IsCursorHidden(long *CursorHidden)
  1076. {
  1077.     CheckPointer(CursorHidden,E_POINTER);
  1078.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1079.     *CursorHidden = (m_bCursorHidden == TRUE ? OATRUE : OAFALSE);
  1080.     return NOERROR;
  1081. }
  1082.  
  1083.  
  1084. // This class implements the IBasicVideo control functions (dual interface)
  1085. // we support a large number of properties and methods designed to allow the
  1086. // client (whether it be an automation controller or a C/C++ application) to
  1087. // set and get a number of video related properties such as the native video
  1088. // size. We support some methods that duplicate the properties but provide a
  1089. // more direct and efficient mechanism as many values may be changed in one
  1090.  
  1091. CBaseControlVideo::CBaseControlVideo(
  1092.                         CBaseFilter *pFilter,        // Owning filter
  1093.                         CCritSec *pInterfaceLock,    // Locking object
  1094.                         TCHAR *pName,                // Object description
  1095.                         LPUNKNOWN pUnk,              // Normal COM ownership
  1096.                         HRESULT *phr) :              // OLE return code
  1097.  
  1098.     CBaseBasicVideo(pName,pUnk),
  1099.     m_pFilter(pFilter),
  1100.     m_pInterfaceLock(pInterfaceLock),
  1101.     m_pPin(NULL)
  1102. {
  1103.     ASSERT(m_pFilter);
  1104.     ASSERT(m_pInterfaceLock);
  1105.     ASSERT(phr);
  1106. }
  1107.  
  1108. // Return an approximate average time per frame
  1109.  
  1110. STDMETHODIMP CBaseControlVideo::get_AvgTimePerFrame(REFTIME *pAvgTimePerFrame)
  1111. {
  1112.     CheckPointer(pAvgTimePerFrame,E_POINTER);
  1113.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1114.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1115.  
  1116.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1117.     if (pVideoInfo == NULL)
  1118.     return E_OUTOFMEMORY;
  1119.     COARefTime AvgTime(pVideoInfo->AvgTimePerFrame);
  1120.     *pAvgTimePerFrame = (REFTIME) AvgTime;
  1121.  
  1122.     return NOERROR;
  1123. }
  1124.  
  1125.  
  1126. // Return an approximate bit rate for the video
  1127.  
  1128. STDMETHODIMP CBaseControlVideo::get_BitRate(long *pBitRate)
  1129. {
  1130.     CheckPointer(pBitRate,E_POINTER);
  1131.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1132.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1133.  
  1134.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1135.     if (pVideoInfo == NULL)
  1136.     return E_OUTOFMEMORY;
  1137.     *pBitRate = pVideoInfo->dwBitRate;
  1138.     return NOERROR;
  1139. }
  1140.  
  1141.  
  1142. // Return an approximate bit error rate
  1143.  
  1144. STDMETHODIMP CBaseControlVideo::get_BitErrorRate(long *pBitErrorRate)
  1145. {
  1146.     CheckPointer(pBitErrorRate,E_POINTER);
  1147.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1148.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1149.  
  1150.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1151.     if (pVideoInfo == NULL)
  1152.     return E_OUTOFMEMORY;
  1153.     *pBitErrorRate = pVideoInfo->dwBitErrorRate;
  1154.     return NOERROR;
  1155. }
  1156.  
  1157.  
  1158. // This returns the current video width
  1159.  
  1160. STDMETHODIMP CBaseControlVideo::get_VideoWidth(long *pVideoWidth)
  1161. {
  1162.     CheckPointer(pVideoWidth,E_POINTER);
  1163.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1164.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1165.  
  1166.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1167.     if (pVideoInfo == NULL)
  1168.     return E_OUTOFMEMORY;
  1169.     *pVideoWidth = pVideoInfo->bmiHeader.biWidth;
  1170.     return NOERROR;
  1171. }
  1172.  
  1173.  
  1174. // This returns the current video height
  1175.  
  1176. STDMETHODIMP CBaseControlVideo::get_VideoHeight(long *pVideoHeight)
  1177. {
  1178.     CheckPointer(pVideoHeight,E_POINTER);
  1179.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1180.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1181.  
  1182.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1183.     if (pVideoInfo == NULL)
  1184.     return E_OUTOFMEMORY;
  1185.     *pVideoHeight = pVideoInfo->bmiHeader.biHeight;
  1186.     return NOERROR;
  1187. }
  1188.  
  1189.  
  1190. // This returns the current palette the video is using as an array allocated
  1191. // by the user. To remain consistent we use PALETTEENTRY fields to return the
  1192. // colours in rather than RGBQUADs that multimedia decided to use. The memory
  1193. // is allocated by the user so we simple copy each in turn. We check that the
  1194. // number of entries requested and the start position offset are both valid
  1195. // If the number of entries evaluates to zero then we return an S_FALSE code
  1196.  
  1197. STDMETHODIMP CBaseControlVideo::GetVideoPaletteEntries(long StartIndex,
  1198.                                                        long Entries,
  1199.                                                        long *pRetrieved,
  1200.                                                        long *pPalette)
  1201. {
  1202.     CheckPointer(pRetrieved,E_POINTER);
  1203.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1204.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1205.     CMediaType MediaType;
  1206.  
  1207.     // Get the video format from the derived class
  1208.  
  1209.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1210.     if (pVideoInfo == NULL)
  1211.     return E_OUTOFMEMORY;
  1212.     BITMAPINFOHEADER *pHeader = HEADER(pVideoInfo);
  1213.  
  1214.     // Is the current format palettised
  1215.  
  1216.     if (PALETTISED(pVideoInfo) == FALSE) {
  1217.         *pRetrieved = 0;
  1218.         return VFW_E_NO_PALETTE_AVAILABLE;
  1219.     }
  1220.  
  1221.     // Do they just want to know how many are available
  1222.  
  1223.     if (pPalette == NULL) {
  1224.         *pRetrieved = pHeader->biClrUsed;
  1225.         return NOERROR;
  1226.     }
  1227.  
  1228.     // Make sure the start position is a valid offset
  1229.  
  1230.     if (StartIndex >= (LONG) pHeader->biClrUsed || StartIndex < 0) {
  1231.         *pRetrieved = 0;
  1232.         return E_INVALIDARG;
  1233.     }
  1234.  
  1235.     // Correct the number we can retrieve
  1236.  
  1237.     LONG Available = (LONG) pHeader->biClrUsed - StartIndex;
  1238.     *pRetrieved = max(0,min(Available,Entries));
  1239.     if (*pRetrieved == 0) {
  1240.         return S_FALSE;
  1241.     }
  1242.  
  1243.     // Copy the palette entries to the output buffer
  1244.  
  1245.     PALETTEENTRY *pEntries = (PALETTEENTRY *) pPalette;
  1246.     RGBQUAD *pColours = COLORS(pVideoInfo) + StartIndex;
  1247.  
  1248.     for (LONG Count = 0;Count < *pRetrieved;Count++) {
  1249.         pEntries[Count].peRed = pColours[Count].rgbRed;
  1250.         pEntries[Count].peGreen = pColours[Count].rgbGreen;
  1251.         pEntries[Count].peBlue = pColours[Count].rgbBlue;
  1252.         pEntries[Count].peFlags = 0;
  1253.     }
  1254.     return NOERROR;
  1255. }
  1256.  
  1257.  
  1258. // This returns the current video dimensions as a method rather than a number
  1259. // of individual property get calls. For the same reasons as said before we
  1260. // cannot access the renderer media type directly as the window object thread
  1261. // may be updating it since dynamic format changes may change these values
  1262.  
  1263. STDMETHODIMP CBaseControlVideo::GetVideoSize(long *pWidth,long *pHeight)
  1264. {
  1265.     CheckPointer(pWidth,E_POINTER);
  1266.     CheckPointer(pHeight,E_POINTER);
  1267.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1268.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1269.  
  1270.     // Get the video format from the derived class
  1271.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1272.     if (pVideoInfo == NULL)
  1273.     return E_OUTOFMEMORY;
  1274.     *pWidth = pVideoInfo->bmiHeader.biWidth;
  1275.     *pHeight = pVideoInfo->bmiHeader.biHeight;
  1276.     return NOERROR;
  1277. }
  1278.  
  1279.  
  1280. // Set the source video rectangle as left,top,right and bottom coordinates
  1281. // rather than left,top,width and height as per OLE automation interfaces
  1282. // Then pass the rectangle on to the window object to set the source
  1283.  
  1284. STDMETHODIMP
  1285. CBaseControlVideo::SetSourcePosition(long Left,long Top,long Width,long Height)
  1286. {
  1287.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1288.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1289.     RECT SourceRect;
  1290.     SourceRect.left = Left;
  1291.     SourceRect.top = Top;
  1292.     SourceRect.right = Left + Width;
  1293.     SourceRect.bottom = Top + Height;
  1294.  
  1295.     // Check the source rectangle is valid
  1296.  
  1297.     HRESULT hr = CheckSourceRect(&SourceRect);
  1298.     if (FAILED(hr)) {
  1299.         return hr;
  1300.     }
  1301.  
  1302.     // Now set the source rectangle
  1303.  
  1304.     hr = SetSourceRect(&SourceRect);
  1305.     if (FAILED(hr)) {
  1306.         return hr;
  1307.     }
  1308.     return OnUpdateRectangles();
  1309. }
  1310.  
  1311.  
  1312. // Return the source rectangle in left,top,width and height rather than the
  1313. // left,top,right and bottom values that RECT uses (and which the window
  1314. // object returns through GetSourceRect) which requires a little work
  1315.  
  1316. STDMETHODIMP
  1317. CBaseControlVideo::GetSourcePosition(long *pLeft,long *pTop,long *pWidth,long *pHeight)
  1318. {
  1319.     // Should check the pointers are non NULL
  1320.  
  1321.     CheckPointer(pLeft,E_POINTER);
  1322.     CheckPointer(pTop,E_POINTER);
  1323.     CheckPointer(pWidth,E_POINTER);
  1324.     CheckPointer(pHeight,E_POINTER);
  1325.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1326.     RECT SourceRect;
  1327.  
  1328.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1329.     GetSourceRect(&SourceRect);
  1330.  
  1331.     *pLeft = SourceRect.left;
  1332.     *pTop = SourceRect.top;
  1333.     *pWidth = WIDTH(&SourceRect);
  1334.     *pHeight = HEIGHT(&SourceRect);
  1335.  
  1336.     return NOERROR;
  1337. }
  1338.  
  1339.  
  1340. // Set the video destination as left,top,right and bottom coordinates rather
  1341. // than the left,top,width and height uses as per OLE automation interfaces
  1342. // Then pass the rectangle on to the window object to set the destination
  1343.  
  1344. STDMETHODIMP
  1345. CBaseControlVideo::SetDestinationPosition(long Left,long Top,long Width,long Height)
  1346. {
  1347.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1348.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1349.     RECT DestinationRect;
  1350.  
  1351.     DestinationRect.left = Left;
  1352.     DestinationRect.top = Top;
  1353.     DestinationRect.right = Left + Width;
  1354.     DestinationRect.bottom = Top + Height;
  1355.  
  1356.     // Check the target rectangle is valid
  1357.  
  1358.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1359.     if (FAILED(hr)) {
  1360.         return hr;
  1361.     }
  1362.  
  1363.     // Now set the new target rectangle
  1364.  
  1365.     hr = SetTargetRect(&DestinationRect);
  1366.     if (FAILED(hr)) {
  1367.         return hr;
  1368.     }
  1369.     return OnUpdateRectangles();
  1370. }
  1371.  
  1372.  
  1373. // Return the destination rectangle in left,top,width and height rather than
  1374. // the left,top,right and bottom values that RECT uses (and which the window
  1375. // object returns through GetDestinationRect) which requires a little work
  1376.  
  1377. STDMETHODIMP
  1378. CBaseControlVideo::GetDestinationPosition(long *pLeft,long *pTop,long *pWidth,long *pHeight)
  1379. {
  1380.     // Should check the pointers are not NULL
  1381.  
  1382.     CheckPointer(pLeft,E_POINTER);
  1383.     CheckPointer(pTop,E_POINTER);
  1384.     CheckPointer(pWidth,E_POINTER);
  1385.     CheckPointer(pHeight,E_POINTER);
  1386.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1387.     RECT DestinationRect;
  1388.  
  1389.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1390.     GetTargetRect(&DestinationRect);
  1391.  
  1392.     *pLeft = DestinationRect.left;
  1393.     *pTop = DestinationRect.top;
  1394.     *pWidth = WIDTH(&DestinationRect);
  1395.     *pHeight = HEIGHT(&DestinationRect);
  1396.  
  1397.     return NOERROR;
  1398. }
  1399.  
  1400.  
  1401. // Set the source left position, the source rectangle we get back from the
  1402. // window object is a true rectangle in left,top,right and bottom positions
  1403. // so all we have to do is to update the left position and pass it back. We
  1404. // must keep the current width constant when we're updating this property
  1405.  
  1406. STDMETHODIMP CBaseControlVideo::put_SourceLeft(long SourceLeft)
  1407. {
  1408.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1409.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1410.     RECT SourceRect;
  1411.     GetSourceRect(&SourceRect);
  1412.     SourceRect.right = SourceLeft + WIDTH(&SourceRect);
  1413.     SourceRect.left = SourceLeft;
  1414.  
  1415.     // Check the source rectangle is valid
  1416.  
  1417.     HRESULT hr = CheckSourceRect(&SourceRect);
  1418.     if (FAILED(hr)) {
  1419.         return hr;
  1420.     }
  1421.  
  1422.     // Now set the source rectangle
  1423.  
  1424.     hr = SetSourceRect(&SourceRect);
  1425.     if (FAILED(hr)) {
  1426.         return hr;
  1427.     }
  1428.     return OnUpdateRectangles();
  1429. }
  1430.  
  1431.  
  1432. // Return the current left source video position
  1433.  
  1434. STDMETHODIMP CBaseControlVideo::get_SourceLeft(long *pSourceLeft)
  1435. {
  1436.     CheckPointer(pSourceLeft,E_POINTER);
  1437.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1438.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1439.     RECT SourceRect;
  1440.  
  1441.     GetSourceRect(&SourceRect);
  1442.     *pSourceLeft = SourceRect.left;
  1443.     return NOERROR;
  1444. }
  1445.  
  1446.  
  1447. // Set the source width, we get the current source rectangle and then update
  1448. // the right position to be the left position (thereby keeping it constant)
  1449. // plus the new source width we are passed in (it expands to the right)
  1450.  
  1451. STDMETHODIMP CBaseControlVideo::put_SourceWidth(long SourceWidth)
  1452. {
  1453.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1454.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1455.     RECT SourceRect;
  1456.     GetSourceRect(&SourceRect);
  1457.     SourceRect.right = SourceRect.left + SourceWidth;
  1458.  
  1459.     // Check the source rectangle is valid
  1460.  
  1461.     HRESULT hr = CheckSourceRect(&SourceRect);
  1462.     if (FAILED(hr)) {
  1463.         return hr;
  1464.     }
  1465.  
  1466.     // Now set the source rectangle
  1467.  
  1468.     hr = SetSourceRect(&SourceRect);
  1469.     if (FAILED(hr)) {
  1470.         return hr;
  1471.     }
  1472.     return OnUpdateRectangles();
  1473. }
  1474.  
  1475.  
  1476. // Return the current source width
  1477.  
  1478. STDMETHODIMP CBaseControlVideo::get_SourceWidth(long *pSourceWidth)
  1479. {
  1480.     CheckPointer(pSourceWidth,E_POINTER);
  1481.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1482.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1483.     RECT SourceRect;
  1484.  
  1485.     GetSourceRect(&SourceRect);
  1486.     *pSourceWidth = WIDTH(&SourceRect);
  1487.     return NOERROR;
  1488. }
  1489.  
  1490.  
  1491. // Set the source top position - changing this property does not affect the
  1492. // current source height. So changing this shunts the source rectangle up and
  1493. // down appropriately. Changing the height complements this functionality by
  1494. // keeping the top position constant and simply changing the source height
  1495.  
  1496. STDMETHODIMP CBaseControlVideo::put_SourceTop(long SourceTop)
  1497. {
  1498.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1499.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1500.     RECT SourceRect;
  1501.     GetSourceRect(&SourceRect);
  1502.     SourceRect.bottom = SourceTop + HEIGHT(&SourceRect);
  1503.     SourceRect.top = SourceTop;
  1504.  
  1505.     // Check the source rectangle is valid
  1506.  
  1507.     HRESULT hr = CheckSourceRect(&SourceRect);
  1508.     if (FAILED(hr)) {
  1509.         return hr;
  1510.     }
  1511.  
  1512.     // Now set the source rectangle
  1513.  
  1514.     hr = SetSourceRect(&SourceRect);
  1515.     if (FAILED(hr)) {
  1516.         return hr;
  1517.     }
  1518.     return OnUpdateRectangles();
  1519. }
  1520.  
  1521.  
  1522. // Return the current top position
  1523.  
  1524. STDMETHODIMP CBaseControlVideo::get_SourceTop(long *pSourceTop)
  1525. {
  1526.     CheckPointer(pSourceTop,E_POINTER);
  1527.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1528.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1529.     RECT SourceRect;
  1530.  
  1531.     GetSourceRect(&SourceRect);
  1532.     *pSourceTop = SourceRect.top;
  1533.     return NOERROR;
  1534. }
  1535.  
  1536.  
  1537. // Set the source height
  1538.  
  1539. STDMETHODIMP CBaseControlVideo::put_SourceHeight(long SourceHeight)
  1540. {
  1541.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1542.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1543.     RECT SourceRect;
  1544.     GetSourceRect(&SourceRect);
  1545.     SourceRect.bottom = SourceRect.top + SourceHeight;
  1546.  
  1547.     // Check the source rectangle is valid
  1548.  
  1549.     HRESULT hr = CheckSourceRect(&SourceRect);
  1550.     if (FAILED(hr)) {
  1551.         return hr;
  1552.     }
  1553.  
  1554.     // Now set the source rectangle
  1555.  
  1556.     hr = SetSourceRect(&SourceRect);
  1557.     if (FAILED(hr)) {
  1558.         return hr;
  1559.     }
  1560.     return OnUpdateRectangles();
  1561. }
  1562.  
  1563.  
  1564. // Return the current source height
  1565.  
  1566. STDMETHODIMP CBaseControlVideo::get_SourceHeight(long *pSourceHeight)
  1567. {
  1568.     CheckPointer(pSourceHeight,E_POINTER);
  1569.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1570.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1571.     RECT SourceRect;
  1572.  
  1573.     GetSourceRect(&SourceRect);
  1574.     *pSourceHeight = HEIGHT(&SourceRect);
  1575.     return NOERROR;
  1576. }
  1577.  
  1578.  
  1579. // Set the target left position, the target rectangle we get back from the
  1580. // window object is a true rectangle in left,top,right and bottom positions
  1581. // so all we have to do is to update the left position and pass it back. We
  1582. // must keep the current width constant when we're updating this property
  1583.  
  1584. STDMETHODIMP CBaseControlVideo::put_DestinationLeft(long DestinationLeft)
  1585. {
  1586.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1587.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1588.     RECT DestinationRect;
  1589.     GetTargetRect(&DestinationRect);
  1590.     DestinationRect.right = DestinationLeft + WIDTH(&DestinationRect);
  1591.     DestinationRect.left = DestinationLeft;
  1592.  
  1593.     // Check the target rectangle is valid
  1594.  
  1595.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1596.     if (FAILED(hr)) {
  1597.         return hr;
  1598.     }
  1599.  
  1600.     // Now set the new target rectangle
  1601.  
  1602.     hr = SetTargetRect(&DestinationRect);
  1603.     if (FAILED(hr)) {
  1604.         return hr;
  1605.     }
  1606.     return OnUpdateRectangles();
  1607. }
  1608.  
  1609.  
  1610. // Return the left position for the destination rectangle
  1611.  
  1612. STDMETHODIMP CBaseControlVideo::get_DestinationLeft(long *pDestinationLeft)
  1613. {
  1614.     CheckPointer(pDestinationLeft,E_POINTER);
  1615.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1616.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1617.     RECT DestinationRect;
  1618.  
  1619.     GetTargetRect(&DestinationRect);
  1620.     *pDestinationLeft = DestinationRect.left;
  1621.     return NOERROR;
  1622. }
  1623.  
  1624.  
  1625. // Set the destination width
  1626.  
  1627. STDMETHODIMP CBaseControlVideo::put_DestinationWidth(long DestinationWidth)
  1628. {
  1629.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1630.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1631.     RECT DestinationRect;
  1632.     GetTargetRect(&DestinationRect);
  1633.     DestinationRect.right = DestinationRect.left + DestinationWidth;
  1634.  
  1635.     // Check the target rectangle is valid
  1636.  
  1637.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1638.     if (FAILED(hr)) {
  1639.         return hr;
  1640.     }
  1641.  
  1642.     // Now set the new target rectangle
  1643.  
  1644.     hr = SetTargetRect(&DestinationRect);
  1645.     if (FAILED(hr)) {
  1646.         return hr;
  1647.     }
  1648.     return OnUpdateRectangles();
  1649. }
  1650.  
  1651.  
  1652. // Return the width for the destination rectangle
  1653.  
  1654. STDMETHODIMP CBaseControlVideo::get_DestinationWidth(long *pDestinationWidth)
  1655. {
  1656.     CheckPointer(pDestinationWidth,E_POINTER);
  1657.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1658.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1659.     RECT DestinationRect;
  1660.  
  1661.     GetTargetRect(&DestinationRect);
  1662.     *pDestinationWidth = WIDTH(&DestinationRect);
  1663.     return NOERROR;
  1664. }
  1665.  
  1666.  
  1667. // Set the target top position - changing this property does not affect the
  1668. // current target height. So changing this shunts the target rectangle up and
  1669. // down appropriately. Changing the height complements this functionality by
  1670. // keeping the top position constant and simply changing the target height
  1671.  
  1672. STDMETHODIMP CBaseControlVideo::put_DestinationTop(long DestinationTop)
  1673. {
  1674.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1675.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1676.     RECT DestinationRect;
  1677.     GetTargetRect(&DestinationRect);
  1678.     DestinationRect.bottom = DestinationTop + HEIGHT(&DestinationRect);
  1679.     DestinationRect.top = DestinationTop;
  1680.  
  1681.     // Check the target rectangle is valid
  1682.  
  1683.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1684.     if (FAILED(hr)) {
  1685.         return hr;
  1686.     }
  1687.  
  1688.     // Now set the new target rectangle
  1689.  
  1690.     hr = SetTargetRect(&DestinationRect);
  1691.     if (FAILED(hr)) {
  1692.         return hr;
  1693.     }
  1694.     return OnUpdateRectangles();
  1695. }
  1696.  
  1697.  
  1698. // Return the top position for the destination rectangle
  1699.  
  1700. STDMETHODIMP CBaseControlVideo::get_DestinationTop(long *pDestinationTop)
  1701. {
  1702.     CheckPointer(pDestinationTop,E_POINTER);
  1703.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1704.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1705.     RECT DestinationRect;
  1706.  
  1707.     GetTargetRect(&DestinationRect);
  1708.     *pDestinationTop = DestinationRect.top;
  1709.     return NOERROR;
  1710. }
  1711.  
  1712.  
  1713. // Set the destination height
  1714.  
  1715. STDMETHODIMP CBaseControlVideo::put_DestinationHeight(long DestinationHeight)
  1716. {
  1717.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1718.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1719.     RECT DestinationRect;
  1720.     GetTargetRect(&DestinationRect);
  1721.     DestinationRect.bottom = DestinationRect.top + DestinationHeight;
  1722.  
  1723.     // Check the target rectangle is valid
  1724.  
  1725.     HRESULT hr = CheckTargetRect(&DestinationRect);
  1726.     if (FAILED(hr)) {
  1727.         return hr;
  1728.     }
  1729.  
  1730.     // Now set the new target rectangle
  1731.  
  1732.     hr = SetTargetRect(&DestinationRect);
  1733.     if (FAILED(hr)) {
  1734.         return hr;
  1735.     }
  1736.     return OnUpdateRectangles();
  1737. }
  1738.  
  1739.  
  1740. // Return the height for the destination rectangle
  1741.  
  1742. STDMETHODIMP CBaseControlVideo::get_DestinationHeight(long *pDestinationHeight)
  1743. {
  1744.     CheckPointer(pDestinationHeight,E_POINTER);
  1745.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1746.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1747.     RECT DestinationRect;
  1748.  
  1749.     GetTargetRect(&DestinationRect);
  1750.     *pDestinationHeight = HEIGHT(&DestinationRect);
  1751.     return NOERROR;
  1752. }
  1753.  
  1754.  
  1755. // Reset the source rectangle to the full video dimensions
  1756.  
  1757. STDMETHODIMP CBaseControlVideo::SetDefaultSourcePosition()
  1758. {
  1759.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1760.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1761.     HRESULT hr = SetDefaultSourceRect();
  1762.     if (FAILED(hr)) {
  1763.         return hr;
  1764.     }
  1765.     return OnUpdateRectangles();
  1766. }
  1767.  
  1768.  
  1769. // Return S_OK if we're using the default source otherwise S_FALSE
  1770.  
  1771. STDMETHODIMP CBaseControlVideo::IsUsingDefaultSource()
  1772. {
  1773.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1774.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1775.     return IsDefaultSourceRect();
  1776. }
  1777.  
  1778.  
  1779. // Reset the video renderer to use the entire playback area
  1780.  
  1781. STDMETHODIMP CBaseControlVideo::SetDefaultDestinationPosition()
  1782. {
  1783.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1784.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1785.     HRESULT hr = SetDefaultTargetRect();
  1786.     if (FAILED(hr)) {
  1787.         return hr;
  1788.     }
  1789.     return OnUpdateRectangles();
  1790. }
  1791.  
  1792.  
  1793. // Return S_OK if we're using the default target otherwise S_FALSE
  1794.  
  1795. STDMETHODIMP CBaseControlVideo::IsUsingDefaultDestination()
  1796. {
  1797.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1798.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1799.     return IsDefaultTargetRect();
  1800. }
  1801.  
  1802.  
  1803. // Return a copy of the current image in the video renderer
  1804.  
  1805. STDMETHODIMP
  1806. CBaseControlVideo::GetCurrentImage(long *pBufferSize,long *pVideoImage)
  1807. {
  1808.     CheckPointer(pBufferSize,E_POINTER);
  1809.     CheckConnected(m_pPin,VFW_E_NOT_CONNECTED);
  1810.     CAutoLock cInterfaceLock(m_pInterfaceLock);
  1811.     FILTER_STATE State;
  1812.  
  1813.     // Make sure we are in a paused state
  1814.  
  1815.     if (pVideoImage != NULL) {
  1816.         m_pFilter->GetState(0,&State);
  1817.         if (State != State_Paused) {
  1818.             return VFW_E_NOT_PAUSED;
  1819.         }
  1820.         return GetStaticImage(pBufferSize,pVideoImage);
  1821.     }
  1822.  
  1823.     // Just return the memory required
  1824.  
  1825.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1826.     if (pVideoInfo == NULL)
  1827.     return E_OUTOFMEMORY;
  1828.     RECT SourceRect;
  1829.     GetSourceRect(&SourceRect);
  1830.     return GetImageSize(pVideoInfo,pBufferSize,&SourceRect);
  1831. }
  1832.  
  1833.  
  1834. // An application has two ways of using GetCurrentImage, one is to pass a real
  1835. // buffer which should be filled with the current image. The other is to pass
  1836. // a NULL buffer pointer which is interpreted as asking us to return how much
  1837. // memory is required for the image. The constraints for when the latter can
  1838. // be called are much looser. To calculate the memory required we synthesize
  1839. // a VIDEOINFO that takes into account the source rectangle that's being used
  1840.  
  1841. HRESULT CBaseControlVideo::GetImageSize(VIDEOINFOHEADER *pVideoInfo,
  1842.                                         LONG *pBufferSize,
  1843.                                         RECT *pSourceRect)
  1844. {
  1845.     NOTE("Entering GetImageSize");
  1846.     ASSERT(pSourceRect);
  1847.  
  1848.     // Check we have the correct input parameters
  1849.  
  1850.     if (pSourceRect == NULL ||
  1851.             pVideoInfo == NULL ||
  1852.             pBufferSize == NULL) {
  1853.  
  1854.         return E_UNEXPECTED;
  1855.     }
  1856.  
  1857.     // Is the data format compatible
  1858.  
  1859.     if (pVideoInfo->bmiHeader.biCompression != BI_RGB) {
  1860.         if (pVideoInfo->bmiHeader.biCompression != BI_BITFIELDS) {
  1861.             return E_INVALIDARG;
  1862.         }
  1863.     }
  1864.  
  1865.     ASSERT(IsRectEmpty(pSourceRect) == FALSE);
  1866.  
  1867.     BITMAPINFOHEADER bih;
  1868.     bih.biWidth = WIDTH(pSourceRect);
  1869.     bih.biHeight = HEIGHT(pSourceRect);
  1870.     bih.biBitCount = pVideoInfo->bmiHeader.biBitCount;
  1871.     LONG Size = DIBSIZE(bih);
  1872.     Size += GetBitmapFormatSize(HEADER(pVideoInfo)) - SIZE_PREHEADER;
  1873.     *pBufferSize = Size;
  1874.  
  1875.     return NOERROR;
  1876. }
  1877.  
  1878.  
  1879. // Given an IMediaSample containing a linear buffer with an image and a type
  1880. // describing the bitmap make a rendering of the image into the output buffer
  1881. // This may be called by derived classes who render typical video images to
  1882. // handle the IBasicVideo GetCurrentImage method. The pVideoImage pointer may
  1883. // be NULL when passed to GetCurrentImage in which case GetImageSize will be
  1884. // called instead, which will just do the calculation of the memory required
  1885.  
  1886. HRESULT CBaseControlVideo::CopyImage(IMediaSample *pMediaSample,
  1887.                                      VIDEOINFOHEADER *pVideoInfo,
  1888.                                      LONG *pBufferSize,
  1889.                                      BYTE *pVideoImage,
  1890.                                      RECT *pSourceRect)
  1891. {
  1892.     NOTE("Entering CopyImage");
  1893.     ASSERT(pSourceRect);
  1894.     BYTE *pCurrentImage;
  1895.  
  1896.     // Check we have an image to copy
  1897.  
  1898.     if (pMediaSample == NULL || pSourceRect == NULL ||
  1899.             pVideoInfo == NULL || pVideoImage == NULL ||
  1900.             pBufferSize == NULL) {
  1901.  
  1902.         return E_UNEXPECTED;
  1903.     }
  1904.  
  1905.     // Is the data format compatible
  1906.  
  1907.     if (pVideoInfo->bmiHeader.biCompression != BI_RGB) {
  1908.         if (pVideoInfo->bmiHeader.biCompression != BI_BITFIELDS) {
  1909.             return E_INVALIDARG;
  1910.         }
  1911.     }
  1912.  
  1913.     ASSERT(IsRectEmpty(pSourceRect) == FALSE);
  1914.  
  1915.     BITMAPINFOHEADER bih;
  1916.     bih.biWidth = WIDTH(pSourceRect);
  1917.     bih.biHeight = HEIGHT(pSourceRect);
  1918.     bih.biBitCount = pVideoInfo->bmiHeader.biBitCount;
  1919.     LONG Size = GetBitmapFormatSize(HEADER(pVideoInfo)) - SIZE_PREHEADER;
  1920.     LONG Total = Size + DIBSIZE(bih);
  1921.  
  1922.     // Make sure we have a large enough buffer
  1923.  
  1924.     if (*pBufferSize < Total) {
  1925.         return E_OUTOFMEMORY;
  1926.     }
  1927.  
  1928.     // Copy the BITMAPINFO
  1929.  
  1930.     CopyMemory((PVOID)pVideoImage, (PVOID)&pVideoInfo->bmiHeader, Size);
  1931.     ((BITMAPINFOHEADER *)pVideoImage)->biWidth = WIDTH(pSourceRect);
  1932.     ((BITMAPINFOHEADER *)pVideoImage)->biHeight = HEIGHT(pSourceRect);
  1933.     ((BITMAPINFOHEADER *)pVideoImage)->biSizeImage = DIBSIZE(bih);
  1934.     BYTE *pImageData = pVideoImage + Size;
  1935.  
  1936.     // Get the pointer to it's image data
  1937.  
  1938.     HRESULT hr = pMediaSample->GetPointer(&pCurrentImage);
  1939.     if (FAILED(hr)) {
  1940.         return hr;
  1941.     }
  1942.  
  1943.     // Now we are ready to start copying the source scan lines
  1944.  
  1945.     LONG ScanLine = (pVideoInfo->bmiHeader.biBitCount / 8) * WIDTH(pSourceRect);
  1946.     LONG LinesToSkip = pVideoInfo->bmiHeader.biHeight;
  1947.     LinesToSkip -= pSourceRect->top + HEIGHT(pSourceRect);
  1948.     pCurrentImage += LinesToSkip * DIBWIDTHBYTES(pVideoInfo->bmiHeader);
  1949.     pCurrentImage += pSourceRect->left * (pVideoInfo->bmiHeader.biBitCount / 8);
  1950.  
  1951.     // Even money on this GP faulting sometime...
  1952.  
  1953.     for (LONG Line = 0;Line < HEIGHT(pSourceRect);Line++) {
  1954.         CopyMemory((PVOID)pImageData, (PVOID)pCurrentImage, ScanLine);
  1955.         pImageData += DIBWIDTHBYTES(*(BITMAPINFOHEADER *)pVideoImage);
  1956.         pCurrentImage += DIBWIDTHBYTES(pVideoInfo->bmiHeader);
  1957.     }
  1958.     return NOERROR;
  1959. }
  1960.  
  1961.  
  1962. // Called when we change media types either during connection or dynamically
  1963. // We inform the filter graph and therefore the application that the video
  1964. // size may have changed, we don't bother looking to see if it really has as
  1965. // we leave that to the application - the dimensions are the event parameters
  1966.  
  1967. HRESULT CBaseControlVideo::OnVideoSizeChange()
  1968. {
  1969.     // Get the video format from the derived class
  1970.  
  1971.     VIDEOINFOHEADER *pVideoInfo = GetVideoFormat();
  1972.     if (pVideoInfo == NULL)
  1973.     return E_OUTOFMEMORY;
  1974.     WORD Width = (WORD) pVideoInfo->bmiHeader.biWidth;
  1975.     WORD Height = (WORD) pVideoInfo->bmiHeader.biHeight;
  1976.  
  1977.     return m_pFilter->NotifyEvent(EC_VIDEO_SIZE_CHANGED,
  1978.                                   MAKELPARAM(Width,Height),
  1979.                                   MAKEWPARAM(0,0));
  1980. }
  1981.  
  1982.  
  1983. // Set the video source rectangle. We must check the source rectangle against
  1984. // the actual video dimensions otherwise when we come to draw the pictures we
  1985. // get access violations as GDI tries to touch data outside of the image data
  1986. // Although we store the rectangle in left, top, right and bottom coordinates
  1987. // instead of left, top, width and height as OLE uses we do take into account
  1988. // that the rectangle is used up to, but not including, the right column and
  1989. // bottom row of pixels, see the Win32 documentation on RECT for more details
  1990.  
  1991. HRESULT CBaseControlVideo::CheckSourceRect(RECT *pSourceRect)
  1992. {
  1993.     CheckPointer(pSourceRect,E_POINTER);
  1994.     LONG Width,Height;
  1995.     GetVideoSize(&Width,&Height);
  1996.  
  1997.     // Check the coordinates are greater than zero
  1998.     // and that the rectangle is valid (left<right, top<bottom)
  1999.  
  2000.     if ((pSourceRect->left >= pSourceRect->right) ||
  2001.        (pSourceRect->left < 0) ||
  2002.        (pSourceRect->top >= pSourceRect->bottom) ||
  2003.        (pSourceRect->top < 0)) {
  2004.  
  2005.         return E_INVALIDARG;
  2006.     }
  2007.  
  2008.     // Check the coordinates are less than the extents
  2009.  
  2010.     if ((pSourceRect->right > Width) ||
  2011.         (pSourceRect->bottom > Height)) {
  2012.  
  2013.         return E_INVALIDARG;
  2014.     }
  2015.     return NOERROR;
  2016. }
  2017.  
  2018.  
  2019. // Check the target rectangle has some valid coordinates, which amounts to
  2020. // little more than checking the destination rectangle isn't empty. Derived
  2021. // classes may call this when they have their SetTargetRect method called to
  2022. // check the rectangle validity, we do not update the rectangles passed in
  2023. // Although we store the rectangle in left, top, right and bottom coordinates
  2024. // instead of left, top, width and height as OLE uses we do take into account
  2025. // that the rectangle is used up to, but not including, the right column and
  2026. // bottom row of pixels, see the Win32 documentation on RECT for more details
  2027.  
  2028. HRESULT CBaseControlVideo::CheckTargetRect(RECT *pTargetRect)
  2029. {
  2030.     // Check the pointer is valid
  2031.  
  2032.     if (pTargetRect == NULL) {
  2033.         return E_POINTER;
  2034.     }
  2035.  
  2036.     // These overflow the WIDTH and HEIGHT checks
  2037.  
  2038.     if (pTargetRect->left > pTargetRect->right ||
  2039.             pTargetRect->top > pTargetRect->bottom) {
  2040.                 return E_INVALIDARG;
  2041.     }
  2042.  
  2043.     // Check the rectangle has valid coordinates
  2044.  
  2045.     if (WIDTH(pTargetRect) <= 0 || HEIGHT(pTargetRect) <= 0) {
  2046.         return E_INVALIDARG;
  2047.     }
  2048.  
  2049.     ASSERT(IsRectEmpty(pTargetRect) == FALSE);
  2050.     return NOERROR;
  2051. }
  2052.  
  2053.